home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / unix / src / stat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-14  |  1.8 KB  |  71 lines

  1. #include "amiga.h"
  2. #include "dir_data.h"
  3. #include <utility/tagitem.h>
  4. #include <sys/stat.h>
  5. #include <string.h>
  6.  
  7. static int attach (char *dest, int dlen, char *dirname, char *name)
  8. {
  9.   char *dirnamep = dirname;
  10.   
  11.   while (*dirnamep && --dlen > 0)
  12.     *dest++ = *dirnamep++;
  13.   /* Add '/' if `dirname' doesn't already end with it. */
  14.   if (dirnamep > dirname && dirnamep[-1] != '/' && dirnamep[-1] != ':' && --dlen > 0)
  15.     *dest++ = '/';
  16.   
  17.   while (*name && --dlen > 0)
  18.     *dest++ = *name++;
  19.   *dest = 0;
  20.   
  21.   return dlen > 0;
  22. }
  23.  
  24. static struct FileInfoBlock fakefib;
  25. static char last_name[256];
  26.  
  27. int stat(char *name, struct stat *sbuf)
  28. {
  29.   struct FileInfoBlock *fib = 0;
  30.   BPTR lock = 0;
  31.   int ret;
  32.   
  33.   chkabort();
  34.   /* See if we want information on the last file returned from readdir */
  35.   if (last_info && last_info->cdir == _get_cd() &&
  36.       attach(last_name, 255, last_info->dirname, last_entry->entry.d_name) &&
  37.       strcmp(last_name, name) == 0)
  38.     {
  39.       /* We do ! Fake a fib */
  40.       
  41.       fakefib.fib_DiskKey = last_entry->entry.d_ino;
  42.       fakefib.fib_NumBlocks = last_entry->numblocks;
  43.       fakefib.fib_Size = last_entry->size;
  44.       fakefib.fib_Date = last_entry->date;
  45.       fakefib.fib_DirEntryType = last_entry->type;
  46.       fakefib.fib_Protection = last_entry->protection;
  47.       
  48.       _fibstat(&fakefib, 0, sbuf);
  49.       ret = 0;
  50.     }    
  51.   else if ((fib = AllocDosObjectTags(DOS_FIB, TAG_END)) &&
  52.        (lock = Lock(name, ACCESS_READ)) &&
  53.        Examine(lock, fib))
  54.     {
  55.       BPTR parent = ParentDir(lock);
  56.       int isroot = !parent;
  57.       
  58.       if (parent) UnLock(parent);
  59.       _fibstat(fib, isroot, sbuf);
  60.       ret = 0;
  61.     }
  62.   else
  63.     {
  64.       ret = -1;
  65.       errno = convert_oserr(IoErr());
  66.     }
  67.   if (lock) UnLock(lock);
  68.   if (fib) FreeDosObject(DOS_FIB, fib);
  69.   return ret;
  70. }
  71.